package model

import (
	j_log "config-client-v1/init/j-log"
	"config-client-v1/utils/tools"
	"encoding/json"
)

func CommandHistoryLog(cmdType uint8, projectID, FSID int, name, detail, url string, param interface{}, resultCode int, resultDetail string) (err error) {
	jsonB, _ := json.Marshal(&param)
	dh := CommandHistory{}
	dh.Type = cmdType
	dh.ProjectID = projectID
	dh.FSID = FSID
	dh.Name = name
	dh.Detail = detail
	dh.Url = url
	dh.Params = string(jsonB)
	dh.ResultCode = resultCode
	dh.ResultDetail = resultDetail
	dh.CreateTime = tools.GetNow()
	err = DB.Create(&dh).Error
	if err != nil {
		return
	}
	return nil
}

type OperationLog struct {
	Id         int      `json:"id" gorm:"column:id"`
	ProjectID  int      `json:"project_id" gorm:"column:project_id"`
	Type       int      `json:"type" gorm:"column:type"`
	Desc       string   `json:"desc" gorm:"column:desc"`
	UID        int      `json:"uid" gorm:"column:uid"`
	CreateTime string   `json:"create_time" gorm:"column:create_time"`
	UserInfo   UserSlim `json:"user_info"  gorm:"foreignKey:UID;references:Id"`
}

func (t *OperationLog) TableName() string {
	return "operation_log"
}

func (t *OperationLog) Log() (err error) {
	t.CreateTime = tools.GetNow()
	err = DB.Omit("user_info").Create(t).Error
	if err != nil {
		j_log.LogWarning([]interface{}{"操作日志记录失败", err, t.Desc})
	}
	return
}
